package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.Service.OrderSettingService;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;


    public void add(List<OrderSetting> list) {
        /*批量导入文件肯能有日期重复的预约设置，所以需要先查表
        若orderDate值已存在则后来的数据要调用edit方法，若无则调用insert*/
        if(list != null && list.size() > 0){
            for (OrderSetting orderSetting : list) {
                long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if(count > 0){
                    orderSettingDao.editByOrderDate(orderSetting);
                }else {
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }


     //通过月份查出当前月的所有预约信息
    public List<Map> getOrderSettingByMonth(String date) {
        String begin = date+"-1";
        String end = date+"-31";
        Map<String,String> m = new HashMap<>();
        m.put("begin",begin);
        m.put("end",end);
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(m);
        List<Map> lists = new ArrayList<>();
        if(list != null && list.size() >0){
            for (OrderSetting orderSetting : list) {
                Map<String,Object> map = new HashMap<>();
                map.put("date",orderSetting.getOrderDate().getDate());//获取天数
                map.put("number",orderSetting.getNumber());
                map.put("reservations",orderSetting.getReservations());
                lists.add(map);
            }
        }
        return lists;
    }


    //通过日期修改对应的预约人数设置
    public void editOrderSettingByDate(OrderSetting orderSetting) {
        if (orderSetting != null) {
            long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
            if(count > 0){
                orderSettingDao.editByOrderDate(orderSetting);
            }else {
                orderSettingDao.add(orderSetting);
            }
        }else {
            new RuntimeException();
        }


    }
}
